﻿using Hangjing.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Hangjing.EleAPIV2
{
    public class ELEShopV2 : ELEAuth
    {
        public ELEShopV2(HttpContext _context) : base(_context)
        {

        }

        public ShopResult getUser(string token)
        {
            ShopResult rs = new ShopResult();


            parameters.Add("nop", "1.0.0");
            parameters.Add("id", Guid.NewGuid().ToString());

            string Timestamp = Utils.getTimestamp();

            string metas = "{\"app_key\": \""+ appconfig.Key + "\",\"timestamp\": "+ Timestamp + "}";

            parameters.Add("metas", JsonConvert.DeserializeObject(metas));


            parameters.Add("params", JsonConvert.DeserializeObject("{}"));
            

            
            parameters.Add("action", "eleme.user.getUser");
            parameters.Add("token", token);


            singparameters.Add("app_key", appconfig.Key);
            singparameters.Add("timestamp", Convert.ToInt64(Timestamp));

            string sign = createMD5Sign(parameters["action"].ToString(), token);

            parameters.Add("signature", sign);


            HttpItem objHttpItem = new HttpItem()
            {
                ContentType = "application/json",
                URL = appconfig.shopapiurl,
                Encoding = "utf-8",
                Method = "POST",
                Postdata = getPostJSONData()
            };

            HttpHelper objhttp = new HttpHelper();
            objhttp.isToLower = false;


            Hangjing.AppLog.AppLog.Info("getUser:" + objHttpItem.Postdata + "\r\nurl=" + appconfig.shopapiurl);

            string returnmsg = objhttp.GetHtml(objHttpItem);

            Hangjing.AppLog.AppLog.Info("getUser:msg=" + returnmsg);

            rs = JsonConvert.DeserializeObject<ShopResult>(returnmsg);



            return rs;

        }

        public OrderResult getOrder(string orderid,string token)
        {
            OrderResult rs = new OrderResult();

         
            parameters.Add("nop", "1.0.0");
            parameters.Add("id", Guid.NewGuid().ToString());

            string Timestamp = Utils.getTimestamp();

            string metas = "{\"app_key\": \"" + appconfig.Key + "\",\"timestamp\": " + Timestamp + "}";

            parameters.Add("metas", JsonConvert.DeserializeObject(metas));


            string paramsdata = "{\"orderId\": \"" + orderid + "\"}";

            parameters.Add("params", JsonConvert.DeserializeObject(paramsdata));



            parameters.Add("action", "eleme.order.getOrder");
            parameters.Add("token", token);


            singparameters.Add("app_key", appconfig.Key);
            singparameters.Add("timestamp", Convert.ToInt64(Timestamp));
            singparameters.Add("orderId", orderid);


            string sign = createMD5Sign(parameters["action"].ToString(), token);

            parameters.Add("signature", sign);


            HttpItem objHttpItem = new HttpItem()
            {
                ContentType = "application/json",
                URL = appconfig.shopapiurl,
                Encoding = "utf-8",
                Method = "POST",
                Postdata = getPostJSONData()
            };

            HttpHelper objhttp = new HttpHelper();
            objhttp.isToLower = false;


            Hangjing.AppLog.AppLog.Info("getOrder:" + objHttpItem.Postdata + "\r\nurl=" + appconfig.shopapiurl);

            string returnmsg = objhttp.GetHtml(objHttpItem);

            Hangjing.AppLog.AppLog.Info("getOrder:msg=" + returnmsg);

            rs = JsonConvert.DeserializeObject<OrderResult>(returnmsg);

            return rs;

        }

    }
}
